package com.briup.ch13;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author briup-adam
 * @Date 2023/10/30 上午10:43
 * @Description 通过构造器对象  创建实例对象
 *
 **/

public class ConstructorNewInstance {
    public static void main(String[] args) {
        Class<Cat> catClass = Cat.class;

        try {
            //获取指定构造器
            Constructor<Cat> constructor1 = catClass.getDeclaredConstructor(String.class);
            //非公有的  添加访问权限 俗称暴力访问
            constructor1.setAccessible(true);
            //通过构造器对象去创建实例
            Cat cat = constructor1.newInstance("学长");
            System.out.println(cat);
            //调用三参构造器  创建 cat实例
            Constructor<Cat> constructor2 = catClass.getDeclaredConstructor(String.class, int.class, String.class);
            //设置可见性
            constructor2.setAccessible(true);
            Cat cat1 = constructor2.newInstance("学长", 3, "白色");
            System.out.println(cat1);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
